package de.preamp.clients.java.playlist;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;

import org.springframework.stereotype.Service;

/**
 * @author André Stannek
 * @since 2010-03-21
 * @version 2011-06-05
 */
@Service("playlistManager")
public class PlaylistManagerInMemoryImpl implements PlaylistManager
{
    /**
     * 
     */
    private static final long serialVersionUID = -5159757278745619312L;

    /**
     * List holding all available playlists.
     */
    private final transient List<Playlist> playlists;

    /**
     * listeners.
     */
    private final transient Set<ListDataListener> listeners;

    /**
     * the currently selected index in this manager.
     */
    private transient int selectedIndex;

    /**
     * Default constructor trying to load the old playlists file or creating a
     * new one.
     */
    public PlaylistManagerInMemoryImpl()
    {
	selectedIndex = -1;
	playlists = new ArrayList<Playlist>();
	listeners = new HashSet<ListDataListener>();
    }

    /*
     * (non-Javadoc)
     * @see de.preamp.clients.java.playlist.PlaylistManager#getPlaylists()
     */
    @Override
    public final Collection<Playlist> getPlaylists()
    {
	return playlists;
    }

    /*
     * (non-Javadoc)
     * @see
     * de.preamp.clients.java.playlist.PlaylistManager#getSelectedPlaylist()
     */
    @Override
    public final Playlist getSelectedPlaylist()
    {
	final Playlist result;
	if (selectedIndex < playlists.size() && selectedIndex >= 0)
	{
	    result = playlists.get(selectedIndex);
	}
	else
	{
	    // TODO: replace dummy playlist
	    result = new PlaylistInMemoryImpl("dummy");
	}
	return result;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Iterable#iterator()
     */
    @Override
    public final Iterator<Playlist> iterator()
    {
	return playlists.iterator();
    }

    /*
     * (non-Javadoc)
     * @see
     * javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event
     * .ListSelectionEvent)
     */
    @Override
    public final void valueChanged(final ListSelectionEvent arg0)
    {
	selectedIndex = arg0.getLastIndex();
    }

    /*
     * (non-Javadoc)
     * @see javax.swing.ListModel#addListDataListener(javax.swing.event.
     * ListDataListener)
     */
    @Override
    public final void addListDataListener(final ListDataListener listener)
    {
	listeners.add(listener);
    }

    /*
     * (non-Javadoc)
     * @see javax.swing.ListModel#getElementAt(int)
     */
    @Override
    public final Object getElementAt(final int index)
    {
	return playlists.get(index);
    }

    /*
     * (non-Javadoc)
     * @see javax.swing.ListModel#getSize()
     */
    @Override
    public final int getSize()
    {
	return playlists.size();
    }

    /*
     * (non-Javadoc)
     * @seejavax.swing.ListModel#removeListDataListener(javax.swing.event.
     * ListDataListener)
     */
    @Override
    public final void removeListDataListener(final ListDataListener listener)
    {
	listeners.remove(listener);
    }

    @Override
    public final void newPlaylist(final String name)
    {
	playlists.add(new PlaylistInMemoryImpl(name));
	for (final ListDataListener listener : listeners)
	{
	    listener.contentsChanged(new ListDataEvent(this,
		    ListDataEvent.INTERVAL_ADDED, playlists.size() - 1,
		    playlists.size() - 1));
	}
    }

    @Override
    public final void removePlaylist(final Playlist list)
    {
	final int index = playlists.indexOf(list);
	playlists.remove(list);
	for (final ListDataListener listener : listeners)
	{
	    listener.contentsChanged(new ListDataEvent(this,
		    ListDataEvent.INTERVAL_REMOVED, index, index));
	}
    }
}
